package main.resources.controller.command.course;

import java.math.BigInteger;

import main.resources.controller.command.Command;
import main.resources.controller.dao.moodle.MdlContextDao;
import main.resources.controller.dao.moodle.MdlCourseCategoryDao;
import main.resources.controller.dao.moodle.MdlCourseDao;
import main.resources.controller.dao.moodle.MdlCourseSectionDao;
import main.resources.exceptions.DaoException;
import main.resources.model.entity.moodle.MdlContext;
import main.resources.model.entity.moodle.MdlCourse;
import main.resources.model.entity.moodle.MdlCourseCategory;
import main.resources.model.entity.moodle.MdlCourseSection;

public class CourseRemoveCommand implements Command {

	private MdlCourse course;
	private String id;
	
	public CourseRemoveCommand(String id) {
		this.id = id;
	}
	
	@Override
	public void execute() throws DaoException {
		this.course = MdlCourseDao.getInstance().find(id);		
		MdlContext context = MdlContextDao.getInstance().filterByCourseAndLevel(new BigInteger(course.getId()), new BigInteger("50")).get(0);
		MdlContextDao.getInstance().remove(context);		
		MdlCourseSection section = MdlCourseSectionDao.getInstance().filterByCourse(new BigInteger(course.getId())).get(0);
		MdlCourseSectionDao.getInstance().remove(section);		
		MdlCourseDao.getInstance().remove(course);
		MdlCourseCategory category = MdlCourseCategoryDao.getInstance().find(course.getCategory().toString());
		category.setCoursecount(BigInteger.valueOf(MdlCourseDao.getInstance().filterByCategory(course.getCategory()).size()));
		MdlCourseCategoryDao.getInstance().edit(category);
	}

}
